// 游学
layui.use(['jquery', 'element', 'table', 'layer', 'form', 'upload'], function(){
    var $ = layui.$;
    var studyAbroadTable = layui.table;
    var ctxPath = "/education";

    studyAbroadTable.render({
        id: 'studyAbroadTableId',
        elem: '#studyAbroadTableId',
        url: ctxPath + '/api/studyAbroad/queryStudyAbroadProducts',
        page: true,
        method: 'post',
        cols:[[
            {field:'id', title:'ID', width: 50},
            {field:'name', title:'名称', width: 400},
            {field:'code', title:'code', width: 100 },
            {field:'generation', title:'年龄段', width: 100 },
            {field:'country', title:'国家', width: 150 },
            {field:'price', title:'价格', width: 100 },
            {field:'deposit', title:'订金', width: 100 },
            {fixed:'right', width: 200, align:'center', toolbar: '#studyAbroadBar'}
        ]],
        response: {
            statusName: 'returnCode' //数据状态的字段名称，默认：code
            ,statusCode: 0 //成功的状态码，默认：0
            ,msgName: 'returnMsg' //状态信息的字段名称，默认：msg
            ,countName: 'count' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        },
        done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);
        }
    });

    var active = {
        searchStudyAbroadProduct: function(){
            var name = $('#name').val();
            var generation = $('#generation').val();
            var country = $('#country').val();
            if(!!name) {
                name = '%' + $('#name').val() + '%';
            }
            studyAbroadTable.reload('studyAbroadTableId', {
                where: {
                    name: name,
                    generation: generation,
                    country: country
                }
            });
        },
        cancelBtn: function(e) {
            window.location.href = ctxPath + '/html/studyAbroad/studyAbroadList.html';
        }
    };

    //按钮事件
    $('.layui-btn').on('click', function(e){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    studyAbroadTable.on('tool(studyAbroad)', function(obj){
        var data = obj.data ,layEvent = obj.event;
        if(layEvent === 'detail'){
            location.href = ctxPath + "/html/studyAbroad/viewStudyAbroad.html?id=" + data.id;
        } else if(layEvent === 'delete'){
            layer.confirm('确定删除？', function(index){
                //向服务端发送删除指令
                $.ajax({
                    url: ctxPath + '/api/studyAbroad/delete',
                    data: {'id' : data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if(result.code == 0) {
                            layer.alert('删除成功！', function(index){
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.close(index);
                            });
                        } else {
                            layer.alert("删除失败：" + result.msg);
                        }
                    }
                });
            });
        } else if(layEvent === 'edit'){
            location.href = ctxPath + "/html/studyAbroad/formStudyAbroad.html?id=" + data.id;
        }
        return false;
    });


});
